import './index.html';
import './styles.less';
import dva from 'dva';
import { useRouterHistory } from 'dva/router';
import { createHashHistory } from 'history';
import createLoading from 'dva-loading';
import { Toast } from 'antd-mobile';

// 1. Initialize
const app = dva({
  // app = dva(opts)
  // opts 包含：
  // history：指定给路由用的 history，默认是 hashHistory
  // initialState：指定初始数据，优先级高于 model 中的 state，默认是 {}
  history: useRouterHistory(createHashHistory)({ queryKey: true }),
  onError(err) {
    
    Toast.hide();
    if (err.body && err.body.Message) {
      if (err.body.Message == '身份信息解析失败') {return;}
      if (err.body.Message == "Bad Request") {
        return Toast.fail('网络错误，请稍候再试');
      }
      Toast.fail(err.body.Message);
    } 
    else if (err.message) {
      if(err.message == "nocheckin") {
        return localStorage.removeItem("auth"+sessionStorage.getItem("hid"));
      }else {
        if (err.message == '身份信息解析失败') {return;}
        if (err.message == "Bad Request") {
          return Toast.fail('网络错误，请稍候再试');
        }
        Toast.fail(err.message);
      }
      
    }
    else {
      Toast.fail('网络错误，请稍候再试');
    }
  },
});

// 2. Plugins
app.use(createLoading());
// 配置 hooks 或者注册插件。（插件最终返回的是 hooks ）

// 3. Model
app.model(require('./models/wechat'));
app.model(require('./models/account'));
app.model(require('./models/profile'));
app.model(require('./models/family'));
app.model(require('./models/message'));
app.model(require('./models/inquiry'));
app.model(require('./models/report'));
app.model(require('./models/article'));
app.model(require('./models/scale'));
app.model(require('./models/food'));
app.model(require('./models/tongue'));
app.model(require('./models/relation'));

// 4. Router
app.router(require('./router'));

// 5. Start
app.start('#root');
